package com.atguigu.tingshu.order.startegy.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.startegy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Service
@Slf4j
public class TrackOrderTradeStrategy implements OrderTradeStrategy {


    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {
        // 判断当前购买集数如果小于0则直接抛出异常
        Integer trackCount = tradeVo.getTrackCount();
        if(trackCount < 0){
            throw new GuiguException(ResultCodeEnum.ILLEGAL_REQUEST);
        }
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        //  获取下单声音列表
        List<TrackInfo> trackInfos = trackInfoFeignClient.findPaidTrackInfoList(tradeVo.getItemId(), trackCount).getData();

        //根据专辑id查询专辑数据
        Long albumId = trackInfos.get(0).getAlbumId();
        AlbumInfo albumInfo = albumInfoFeignClient.getAlbumInfo(albumId).getData();

        //计算原始价格 声音购买没有折扣
        BigDecimal originalAmount = trackCount > 0 ? albumInfo.getPrice().multiply(new BigDecimal(trackCount)) : albumInfo.getPrice();

        orderInfoVo.setItemType(SystemConstant.ORDER_ITEM_TYPE_TRACK);

        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setDerateAmount(new BigDecimal("0.00"));
        orderInfoVo.setOrderAmount(originalAmount);

        if(!CollectionUtils.isEmpty(trackInfos)){
            List<OrderDetailVo> orderDetailVoList = trackInfos.stream().map(trackInfo -> {
                OrderDetailVo orderDetailVo = new OrderDetailVo();
                orderDetailVo.setItemId(trackInfo.getId());
                orderDetailVo.setItemName(trackInfo.getTrackTitle());
                orderDetailVo.setItemUrl(trackInfo.getCoverUrl());
                orderDetailVo.setItemPrice(albumInfo.getPrice());
                return orderDetailVo;
            }).collect(Collectors.toList());
            orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        }

        orderInfoVo.setOrderDerateVoList(Arrays.asList());

        this.setOtherDataToOrderInfoVo(orderInfoVo,stringRedisTemplate);

        return orderInfoVo;
    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_TRACK;
    }
}
